package api

import (
	"github.com/gin-gonic/gin"
	"go-malphite/app/common/constant"
	"go-malphite/app/common/dto"
	"go-malphite/app/common/resp"
	"go-malphite/app/model"
	"go-malphite/app/service"
	"strconv"
)

// AddTodo
// @Summary 新增待办
// @Tags 待办模块
// @Param Authorization header string true "JWT"
// @Param req body model.Todo false "新增待办参数"
// @Success 200 {object} resp.Response{}
// @Router /api/v1/todo/add [post]
func AddTodo(c *gin.Context) {
	var params model.TodoAddParams
	err := c.ShouldBindJSON(&params)
	if err != nil {
		resp.FailWithMsg(c, "请求参数格式错误！")
		return
	}
	if len(params.Content) <= 0 {
		resp.FailWithMsg(c, "待办内容不能为空！")
		return
	}
	if len(params.Content) > 255 {
		resp.FailWithMsg(c, "待办内容长度不能超过255个字符！")
		return
	}

	todo := model.Todo{
		ID:      dto.GenID(),
		Status:  false,
		Content: params.Content,
		UserId:  c.GetInt64(constant.CurrentUserId),
	}
	flag := service.InsertTodo(todo)
	if flag {
		resp.OkWithMsg(c, "新增成功！")
		return
	}
	resp.FailWithMsg(c, "新增失败！")
}

// RemoveTodo
// @Summary 删除待办
// @Tags 待办模块
// @Param Authorization header string true "JWT"
// @Param id path string false "待办id"
// @Success 200 {object} resp.Response{}
// @Router /api/v1/todo/remove/:id [delete]
func RemoveTodo(c *gin.Context) {
	todoId, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		resp.FailWithMsg(c, "待办id格式错误！")
		return
	}

	todo, err := service.FindTodoById(todoId)
	if err != nil {
		resp.FailWithMsg(c, "待办不存在，删除失败！")
		return
	}

	if todo.UserId != c.GetInt64(constant.CurrentUserId) {
		resp.FailWithMsg(c, "当前用户无权进行此操作！")
		return
	}

	flag := service.RemoveTodo(todoId)
	if flag {
		resp.OkWithMsg(c, "删除成功！")
		return
	}
	resp.FailWithMsg(c, "删除失败！")
}

// UpdateTodo
// @Summary 修改待办
// @Tags 待办模块
// @Param Authorization header string true "JWT"
// @Success 200 {object} resp.Response{}
// @Router /api/v1/todo/update/:id [put]
func UpdateTodo(c *gin.Context) {
	todoId, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		resp.FailWithMsg(c, "待办id格式错误！")
		return
	}

	todo, err := service.FindTodoById(todoId)
	if err != nil {
		resp.FailWithMsg(c, "待办不存在，更新失败！")
		return
	}

	if todo.UserId != c.GetInt64(constant.CurrentUserId) {
		resp.FailWithMsg(c, "当前用户无权进行此操作！")
		return
	}

	todo.Status = true

	flag := service.UpdateTodo(todo)
	if flag {
		resp.OkWithMsg(c, "更新成功！")
		return
	}
	resp.FailWithMsg(c, "更新失败！")
}

// GetDetailTodo
// @Summary 获取待办详情
// @Tags 待办模块
// @Param Authorization header string true "JWT"
// @Param id path string false "待办id"
// @Success 200 {object} resp.Response{data=model.TodoOneRes}
// @Router /api/v1/todo/detail/:id [put]
func GetDetailTodo(c *gin.Context) {
	todoId, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		resp.FailWithMsg(c, "待办id格式错误！")
		return
	}

	data, err := service.GetDetailTodo(todoId, c.GetInt64(constant.CurrentUserId))
	if err != nil {
		resp.FailWithMsg(c, "查询失败！原因："+err.Error())
		return
	}
	resp.OkWithData(c, data, "查询成功！")
}

// GetListTodo
// @Summary 获取待办列表
// @Tags 待办模块
// @Param Authorization header string true "JWT"
// @Success 200 {object} resp.Response{}
// @Router /api/v1/todo/list [get]
func GetListTodo(c *gin.Context) {
	dataList, err := service.GetListTodo(c.GetInt64(constant.CurrentUserId))
	if err != nil {
		resp.FailWithMsg(c, "查询失败！")
		return
	}
	resp.OkWithData(c, dataList, "查询成功！")
}

// GetPageTodo
// @Summary 获取待办列表-分页
// @Tags 待办模块
// @Param Authorization header string true "JWT"
// @Param req body dto.PageReq false "分页"
// @Success 200 {object} resp.Response{}
// @Router /api/v1/todo/page [get]
func GetPageTodo(c *gin.Context) {
	data, err := service.GetPageTodo(c.GetInt64(constant.CurrentUserId), dto.GetPageReq(c))
	if err != nil {
		resp.FailWithMsg(c, "查询失败！")
		return
	}
	resp.OkWithData(c, data, "查询成功！")
}
